package com.example.finalsystem.Service;

import com.example.finalsystem.Dao.DealMapper;
import com.example.finalsystem.Dao.ListGoodsMapper;
import com.example.finalsystem.Dao.OutListMapper;
import com.example.finalsystem.Entity.ListGoods;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class ListGoodsService {

    @Autowired
    OutListService outListService;
    private SqlSession openSession()
    {
        String resource = "mybatis-config.xml";
        InputStream is =ListGoodsService.class.getClassLoader().getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        return   sessionFactory.openSession(true);
    }


    private Map<String,Integer> parseGoods(String goods)
    {
        Map<String,Integer> map = new HashMap<>();
        String [] goodTypes =goods.split(";");
        for (String s: goodTypes
             ) {
            String [] tmp = s.split("#");
            map.put(tmp[0],Integer.parseInt(tmp[1]));
        }
        return map;
    }
//    显微镜!1#1
    public String addListGoods(String dealId,String goods)
    {
        SqlSession sqlSession = openSession();
        Map<String,Integer> map = parseGoods(goods);
        String key;
        Integer value;
        String []tmp;
        for (Map.Entry<String, Integer> entry: map.entrySet()) {

            key = entry.getKey();
            value = entry.getValue();
            tmp = key.split("!");
            ListGoods listGoods = new ListGoods();
            listGoods.setGoodsname(tmp[0]);
            listGoods.setGoodsnumber(value);
            listGoods.setFkDealid(dealId);
            listGoods.setGoodsprice(Double.parseDouble(tmp[1]));
            sqlSession.getMapper(ListGoodsMapper.class).insert(listGoods);
        }
        sqlSession.commit();
        sqlSession.close();
        return "添加成功";
    }
    public String fixListGoodsById(Long listGoodsId,Integer goodsNumber,Double goodsPrice,String goodsName)
    {
        SqlSession sqlSession = openSession();
        ListGoods listGoods = sqlSession.getMapper(ListGoodsMapper.class).selectByPrimaryKey(listGoodsId);
        listGoods.setGoodsprice(goodsPrice);
        listGoods.setGoodsnumber(goodsNumber);
        listGoods.setGoodsname(goodsName);
        sqlSession.getMapper(ListGoodsMapper.class).updateByPrimaryKey(listGoods);
        sqlSession.commit();
        sqlSession.close();
        return "修改成功";
    }
    public String deleteByListGoodsId(Long listGoodsId)
    {
        SqlSession sqlSession = openSession();
        sqlSession.getMapper(ListGoodsMapper.class).deleteByPrimaryKey(listGoodsId);
        sqlSession.commit();
        sqlSession.close();
        return "删除成功";
    }
    public void deleteAllByDealId(String DealId)
    {
        SqlSession sqlSession = openSession();
        sqlSession.getMapper(ListGoodsMapper.class).deleteAllByFK_dealId(DealId);
        sqlSession.commit();
        sqlSession.close();
    }
    public ListGoods getListGoods(Long listId)
    {
        SqlSession sqlSession = openSession();
        ListGoods listGoods = sqlSession.getMapper(ListGoodsMapper.class).selectByPrimaryKey(listId);
        sqlSession.close();
        return listGoods;
    }
    public List<ListGoods> getListGoodsByDealId(String dealId,SqlSession sqlSession)
    {
        List<ListGoods> listGoods =  sqlSession.getMapper(ListGoodsMapper.class).getAllByFK_dealId( dealId);
        for (ListGoods l:listGoods
             ) {
            l.setGoodsnumber(l.getGoodsnumber());
            l.setTempNumber(outListService.countBuyNumber(l.getListgoodsid()));
        }
        return  listGoods;
    }
}
